using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GamePoolObject
{
	private GamePoolSettings GamePoolSettings;

	private GameObject Prefab;

	private List<GamePoolTracker> GamePoolTrackerList;

	private GameObject _poolHolder;

	private int _lastIndex;

	private int _objectId;

	public int ObjectId
	{
		get
		{
			return _objectId;
		}
	}

	public GamePoolObject(GameObject iPoolHolder, int iObjectId, GameObject iGameObject, int iCount, GamePoolSettings iGamePoolSettings)
	{
		_poolHolder = iPoolHolder;
		_objectId = iObjectId;
		Prefab = iGameObject;
		_lastIndex = -1;
		GamePoolSettings = iGamePoolSettings;
		GamePoolTrackerList = new List<GamePoolTracker>();
		AddObjects(iCount);
	}

	public void AddObjects(int iCount)
	{
		for (int i = 0; i < iCount; i++)
		{
			AddOneObject();
		}
	}

	private GamePoolTracker AddOneObject()
	{
		GameObject gameObject = Object.Instantiate(Prefab, Vector3.zero, Quaternion.identity) as GameObject;
		gameObject.name = Prefab.name;
		gameObject.transform.parent = _poolHolder.transform;
		GamePoolTracker gamePoolTracker = gameObject.AddComponent<GamePoolTracker>();
		gamePoolTracker.GamePoolObject = this;
		GamePoolTrackerList.Add(gamePoolTracker);
		gameObject.SetActive(false);
		return gamePoolTracker;
	}

	public GameObject GetFreeObject()
	{
		int count = GamePoolTrackerList.Count;
		int num = _lastIndex;
		for (int i = 0; i < count; i++)
		{
			num++;
			if (num >= count)
			{
				num = 0;
			}
			if (!GamePoolTrackerList[num].IsBusy)
			{
				_lastIndex = num;
				GamePoolTrackerList[num].IsBusy = true;
				return GamePoolTrackerList[num].gameObject;
			}
		}
		_lastIndex = count;
		GamePoolTracker gamePoolTracker = AddOneObject();
		gamePoolTracker.IsBusy = true;
		return gamePoolTracker.gameObject;
	}

	public void ReturnGameObject(GamePoolTracker iGamePoolTracker)
	{
		if (!(iGamePoolTracker == null) && !(iGamePoolTracker.gameObject == null) && !(_poolHolder == null))
		{
			iGamePoolTracker.transform.parent = _poolHolder.transform;
			iGamePoolTracker.transform.localPosition = Vector3.zero;
			iGamePoolTracker.gameObject.SendMessage("OnPoolDeactivate", SendMessageOptions.DontRequireReceiver);
			iGamePoolTracker.StartCoroutine(DeactivateObject(iGamePoolTracker));
		}
	}

	private IEnumerator DeactivateObject(GamePoolTracker iGamePoolTracker)
	{
		yield return null;
		iGamePoolTracker.IsBusy = false;
		iGamePoolTracker.gameObject.SetActive(false);
		if (GamePoolSettings.IsUseAverageCount && GamePoolTrackerList.Count > GamePoolSettings.AverageCount)
		{
			GamePoolTrackerList.Remove(iGamePoolTracker);
			iGamePoolTracker.transform.parent = null;
			Object.Destroy(iGamePoolTracker.gameObject);
		}
	}

	public void Clear()
	{
		int num;
		for (num = 0; num < GamePoolTrackerList.Count; num++)
		{
			if (GamePoolTrackerList[num].IsBusy)
			{
				ReturnGameObject(GamePoolTrackerList[num]);
			}
			GamePoolTrackerList[num].GamePoolObject = null;
			Object.Destroy(GamePoolTrackerList[num].gameObject);
			GamePoolTrackerList[num] = null;
			GamePoolTrackerList.RemoveAt(num);
			num--;
		}
		GamePoolTrackerList.Clear();
	}
}
